<?php
namespace app\admin\controller;
use app\BaseController;

use app\common\model\RecordModel;

class Visitor extends BaseController{
    public function index()
    {
        $where=[
            ['firm_id','=',$this->request->firm_id]
        ];
        $staff_id = $this->request->param('staff_id/d',0);
        if($staff_id > 0){
            $where[]=['staff_id','=',$staff_id];
        }
        $start_time = $this->request->param('start/s','');
        $end_time = $this->request->param('end/s','');
        
        if(!empty($start_time) && !empty($end_time)){
            $where[]=['add_time','>=',strtotime($start_time.' 00:00:00')];
            $where[]=['add_time','<=',strtotime($end_time.' 23:59:59')];
        }
        $list = RecordModel::with(['user','staff'])
            ->where($where)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('record_id','desc')
            ->select();
        $total = RecordModel::where($where)->count('record_id');
        
        return success([
            'list' => $list,
            'total' => $total
        ]);
    }
    
    public function export()
    {
        $where=[
            ['firm_id','=',$this->request->firm_id]
        ];
        $staff_id = $this->request->param('staff_id/d',0);
        if($staff_id > 0){
            $where[]=['staff_id','=',$staff_id];
        }
        $start_time = $this->request->param('start/s','');
        $end_time = $this->request->param('end/s','');
        
        if(!empty($start_time) && !empty($end_time)){
            $where[]=['add_time','>=',strtotime($start_time.' 00:00:00')];
            $where[]=['add_time','<=',strtotime($end_time.' 23:59:59')];
        }
        $list = RecordModel::with(['user','staff'])
            ->where($where)
            ->order('record_id','desc')
            ->select();
        //处理数据
        $csvArr=[];
        if(count($list) > 0){
            foreach ($list as $k => $row){
                $csvArr[]=[
                    $row['create_time'].chr(20),
                    $row['name'],
                    $row['open_id'],
                    $row['mobile'],
                    $row['user_name'],
                    $row['duration'],
                    $row['info']
                ];
            }
        }
        // die;
        $this->download_send_headers("data_export_" . date("Y-m-d") . ".csv");
        $head=array('访问时间','员工姓名','访客open_id','手机号','昵称','访问时长','备注');
        echo $this->array2csv($csvArr,$head);
        unset($csvArr);
        die();

    }

    protected  function array2csv(array &$array,$head)
    {
        if (count($array) == 0) {
            return null;
        }
        ob_start();
        $df = fopen("php://output", 'w');
        if(!$head){
            $head=array_keys(reset($array));
        }
        fputcsv($df,$head);
        foreach ($array as $row) {
            fputcsv($df, $row);
        }
        fclose($df);
        return ob_get_clean();
    }

    protected function download_send_headers($filename)
    {
        $now = gmdate("D, d M Y H:i:s");
        header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
        header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
        header("Last-Modified: {$now} GMT");
        // force download
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        // disposition / encoding on response body
        header("Content-Disposition: attachment;filename={$filename}");
        header("Content-Transfer-Encoding: binary");
    }
    

}
